package cn.itlaobing.springmvc.web.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.itlaobing.springmvc.entity.Account;
import cn.itlaobing.springmvc.service.AccountService;

/**
 * 登陆，退出与会话相关的控制器
 * @author wq
 *
 */
@Controller
public class SessionController extends BaseController{
	public static final String CURRENT_ACCOUNT="currentAccount";
	
	@Autowired
	private AccountService accountServcie;
	
	
	@RequestMapping(value="/signIn",method=RequestMethod.GET)
	public String signIn(){
		return "signIn";
	}
	
	@RequestMapping(value="/signOut",method=RequestMethod.GET)
	public String signOut(HttpSession session){
		
		//让session失效
		session.invalidate();
		return "redirect:/signIn";
	}
	
	
	@RequestMapping(value="/signIn",method=RequestMethod.POST)
	public String signIn(HttpSession session,
			String account,String password,
			Model model){
		
		Account acc=accountServcie.validate(account, password);
		
		
		
		
		if(acc!=null){  //登录成功
			session.setAttribute(CURRENT_ACCOUNT, acc);
			//更新最后一次登录的时间
			accountServcie.updateLastSignInDateTime(acc.getAccount());
			return "redirect:/home";
		}else{
			model.addAttribute("signInError","用户名或者密码有误");
			return "signIn";
		}
		
	}
	
}
